<template>
  <el-form class="login-form"
           status-icon
           :rules="loginRules"
           ref="loginForm"
           :model="loginForm"
           label-width="0">
    <el-form-item prop="account2">
      <el-input size="small"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.account2"
                auto-complete="off"
                :placeholder="$t('请输入账号')"
                :disabled = "disabled"
                ref="account2"
                >
        <i slot="prefix"
           class="el-icon-user"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input size="small"
                @keyup.enter.native="handleLogin"
                :type="passwordType"
                v-model="loginForm.password"
                auto-complete="off"
                :placeholder="$t('请输入密码')"
                :disabled = "disabled"
                ref="password"
                >
        <i class="el-icon-view el-input__icon"
           slot="suffix"
           @click="showPassword"></i>
        <i slot="prefix"
           class="el-icon-lock"></i>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"
                 @click.native.prevent="handleLogin"
                 :loading="isLoading"
                 class="login-submit">{{submitName}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
	
	//admin@taobao
	//system@saas
	
import { mapGetters } from "vuex"
export default {
  name: "userlogin",
  data() {
    return {
      loginForm: {
        account: "",
		account2: "system@saas",
        password: "0000",
		tenant:""
      },
      loginRules: {
        account2: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, message: "密码长度最少为4位", trigger: "blur" }
        ]
      },
      passwordType: "password",
      isLoading:false,
      disabled : false,
      submitName: this.$t('登录')
    }
  },
  created() {
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["tagWel"])
  },
  props: [],
  methods: {
    showPassword() {
      this.passwordType == ""
        ? (this.passwordType = "password")
        : (this.passwordType = "")
    },
    loading(flag){
      if(flag){
          this.submitName = this.$t('登录中...')
          this.isLoading = true
          this.disabled = true
      }
      else{
            this.submitName = this.$t('登录')
            this.isLoading = false
            this.disabled = false
      }
    },
    handleLogin() {
	  var _this=this;
      this.$refs.loginForm.validate((valid,object) => {
        if (valid) {
			var accout=_this.loginForm.account2;
		  if(accout.lastIndexOf("@")!=-1){
			  var index=accout.lastIndexOf("@");
			  var str=accout.substring(0,index);
			  var str2=accout.substring(index+1);
			  _this.loginForm.account=str;
			  _this.loginForm.tenant=str2;
		  }else{
			  _this.$message({
				message: '用户名缺少@符号',
				type: 'warning'
			  });
			  return false;
		  }
		  this.loading(true);
		  //organization/api/user.js 文件路径
          this.$store.dispatch("organization/user/login", this.loginForm).then((res) => {
            this.$store.commit('app/SET_ISFIRSTLOAD',true)
            this.$router.push({ path: this.tagWel.value })
          }).catch(()=>{
            this.loading(false)
          })
        }
        else{
          const id = object.password ? 'password' :'account'
          this.$refs[id].focus()
        }
      })
    }
  }
}
</script>
